function preLoad() {
	if (!this.support.loading) {
		alert("You need the Flash Player to use SWFUpload.");
		return false;
	}/* else if (!this.support.imageResize) {
		alert("You need Flash Player 10 to upload resized images.");
		return false;
	}*/
	else{
		//get num of max upload
		var numOfMaxUpload=1;
		if(this.customSettings.numOfMaxUpload != undefined)numOfMaxUpload=this.customSettings.numOfMaxUpload;
		if(numOfMaxUpload<0)numOfMaxUpload=1;
		this.numOfMaxUpload=numOfMaxUpload;		
		if(this.numOfFileUploaded == undefined)this.numOfFileUploaded=0;	
		this.fileError=new Array();
	}
	
	
}
function loadFailed() {
	alert("Something went wrong while loading SWFUpload. If this were a real application we'd clean up and then give you an alternative");
}

function fileQueueError(file, errorCode, message) {
	try {
		var imageName = "error.gif";
		var errorName = "";
		if (errorCode === SWFUpload.errorCode_QUEUE_LIMIT_EXCEEDED) {
			errorName = "You have attempted to queue too many files.";
		}

		if (errorName !== "") {
			alert(errorName);
			return;
		}

		switch (errorCode) {
		case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
			imageName = "zerobyte.gif";
			break;
		case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
			imageName = "toobig.gif";
			break;
		case -100:	
			alert("Upload exceeds!");
			break;
		case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
		case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
		default:
			alert(message);
			break;
		}

		addImage("images/" + imageName);

	} catch (ex) {
		this.debug(ex);
	}

}

function fileDialogComplete(numFilesSelected, numFilesQueued) {
	try {
		if (numFilesQueued > 0) {
			//checking
			//check max upload
			if(this.numOfFileUploaded >=this.numOfMaxUpload && this.numOfMaxUpload!=0){
				alert("Maximum upload exceeds.");
				return false;
			}
			else
			{			
				
				if(this.customSettings.uploadType=="file"){				
					this.startUpload();
				}
				else{
				    this.startUpload();
                    // for flash 10
                    //this.startResizedUpload(this.getFile(0).ID, this.customSettings.thumbnail_width, this.customSettings.thumbnail_height, SWFUpload.RESIZE_ENCODING.JPEG, this.customSettings.thumbnail_quality, false);
				}
			}			
		}
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadProgress(file, bytesLoaded) {

	try {
		var percent = Math.ceil((bytesLoaded / file.size) * 100);
		var progress = new FileProgress(file,  this.customSettings);
		progress.setProgress(percent);
		progress.setStatus("Uploading...");
		progress.toggleCancel(true, this);
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadSuccess(file, serverData) {
	try {
		var progress = new FileProgress(file,  this.customSettings);
		if (serverData.substring(0, 7) === "FILEID:") {
		    var filenamePath = serverData.substring(7);
		    filename = filenamePath.substring(filenamePath.lastIndexOf("/") + 1);
		    var filenamenoext = filenamePath.substring(filenamePath.lastIndexOf("/") + 1, filenamePath.lastIndexOf("."));
			var inputFieldData ="";
			if(this.customSettings.fieldInputId !=undefined){
				inputFieldData = document.getElementById(this.customSettings.fieldInputId);
				inputFieldData.value =filename;
			}	
			if(this.customSettings.type==1){
				progress.addImage(filenamePath);
				progress.setHiddenField(filename);
			}
			else if(this.customSettings.type==2){
				progress.setHiddenField(filename);
				progress.addImage(filenamePath);
				this.customSettings.filename=filename;
			}
			progress.setStatus("Upload Complete.");
			progress.toggleCancel(false);	
			this.fileError[file.index]=0;
				
		} else {			
			
			progress.setStatus("Error.");
			progress.addImage(SITE_URL+"/img/error.gif");
			progress.toggleCancel(false);	
			progress.setError();
			this.fileError[file.index]=1;
			alert(serverData);
		}


	} catch (ex) {
		this.debug(ex);
	}
}

function uploadComplete(file) {
	try {
		//count num file of upload
		
		if(!this.fileError[file.index]){
			var progress = new FileProgress(file,  this.customSettings);
			progress.setComplete();
			progress.toggleCancel(false);
		}
			/*  I want the next upload to continue automatically so I'll call startUpload here */		
			if (this.getStats().files_queued > 0 && (this.numOfMaxUpload==0 || this.numOfFileUploaded < this.numOfMaxUpload)) {
				
				this.startResizedUpload(this.getFile(0).ID, this.customSettings.thumbnail_width, this.customSettings.thumbnail_height, SWFUpload.RESIZE_ENCODING.JPEG, this.customSettings.thumbnail_quality, false);
			}
		
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadError(file, errorCode, message) {
	var imageName =  "error.gif";
	var progress;
	try {
		switch (errorCode) {
		case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
			try {
				progress = new FileProgress(file,  this.customSettings);
				progress.setCancelled();
				progress.setStatus("Cancelled");
				progress.toggleCancel(false);
			}
			catch (ex1) {
				this.debug(ex1);
			}
			break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
			try {
				progress = new FileProgress(file,  this.customSettings);
				progress.setCancelled();
				progress.setStatus("Stopped");
				progress.toggleCancel(true);
			}
			catch (ex2) {
				this.debug(ex2);
			}
		case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
			imageName = "uploadlimit.gif";
			break;
		default:
			try {
				progress = new FileProgress(file,  this.customSettings);
				progress.setCancelled();
				progress.setStatus("Error");
				progress.toggleCancel(true);
				progress.addImage("images/" + imageName)
			}
			catch (ex2) {
				this.debug(ex2);
			}
			break;
		}

		

	} catch (ex3) {
		this.debug(ex3);
	}

}


function addImageGallery(src,caller) {
	try {
		var filenamenoext=src.substring(src.lastIndexOf('/')+1,src.lastIndexOf('.'));
		var defaultDivThumb='<div id="imageUpload" class="imageUpload"></div>';		
		var divThumb='';

		if(caller.customSettings.uploadDefaultDivThumb!=undefined)
			defaultDivThumb=caller.customSettings.uploadDefaultDivThumb;		
		defaultDivThumb=$(defaultDivThumb);
		if(caller.customSettings.test2ReplaceWithImageId!=undefined){
			defaultDivThumb.html(defaultDivThumb.html().replace(caller.customSettings.test2ReplaceWithImageId,filenamenoext));
			if (defaultDivThumb.attr('id')==null)
				defaultDivThumb.attr('id',filenamenoext);
			else{
				defaultDivThumb.attr('id',defaultDivThumb.attr('id').replace(caller.customSettings.test2ReplaceWithImageId,filenamenoext));				
			}
		}

		if(caller.customSettings.DivThumbIdContainUploadImage!=undefined){
			divThumb=defaultDivThumb.find('#'+caller.customSettings.DivThumbIdContainUploadImage);
		}
		else{
			divThumb=defaultDivThumb;
		}	
		var divContain="";		
		if(caller.customSettings.uploadContainer!=undefined)
			divContain=$('#'+caller.customSettings.uploadContainer);
		divContain.append(defaultDivThumb);

		//if have divcontain then add div into that
		if(divContain!=""){			
			var newImg=document.createElement("img");
			divThumb.append(newImg);
			//document.getElementById("thumbnails").appendChild(newImg);
			if (newImg.filters) {
				try {
					newImg.filters.item("DXImageTransform.Microsoft.Alpha").opacity = 0;
				} catch (e) {
					// If it is not set initially, the browser will throw an error.  This will set it if it is not set yet.
					newImg.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + 0 + ')';
				}
			} else {
				newImg.style.opacity = 0;
			}
		
			newImg.onload = function () {
				fadeIn(newImg, 0);
			};
			newImg.src = src;
		}

	} catch (ex) {
		this.debug(ex);
	}
}

function addImageSingle(src,caller) {
	try {
		
		if(caller.customSettings.uploadContainer!=undefined){
			var divThumb=$('#'+caller.customSettings.uploadContainer);
			//divThumb.html('');
			
			//var newImg=document.createElement("img");
			var newImg=divThumb.find('img');
			if(newImg.attr('src')==undefined){
				newImg=document.createElement("img");
				divThumb.children().first().before(newImg);
				if (newImg.filters) {
					try {
						newImg.filters.item("DXImageTransform.Microsoft.Alpha").opacity = 0;
					} catch (e) {
						// If it is not set initially, the browser will throw an error.  This will set it if it is not set yet.
						newImg.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + 0 + ')';
					}
				} else {
					newImg.style.opacity = 0;
				}				
				newImg.onload = function () {
					fadeIn(newImg, 0);
				};
				newImg.src =src;
			}
			else{
				newImg.attr('src',src);
			}
			
			
		}

	} catch (ex) {
		this.debug(ex);
	}
}





